﻿using System.Windows;
using System.Windows.Controls;

namespace Comarch.Animation
{
    internal static class TranslationCalculator
    {
        public static Point GetCenter(FrameworkElement target)
        {
            var rootPanel = target.FindRoot<Panel>();
            rootPanel.UpdateLayout();
            var leftCorner = target.TransformToVisual(rootPanel).Transform(new Point());
            var width = target.ActualWidth/2.0;
            var height = target.ActualHeight/2.0;

            return new Point(leftCorner.X+ width, leftCorner.Y+height);
        }

        public static int CalculateXTranslateDistance(FrameworkElement target, FrameworkElement destination)
        {
            return (int)(GetCenter(destination).X - GetCenter(target).X);
        }

        public static int CalculateYTranslateDistance(FrameworkElement target, FrameworkElement destination)
        {
            return (int)(GetCenter(destination).Y - GetCenter(target).Y);
        }

        public static int CalculateInitialXOffset(int originalOffset, FlyDirection direction)
        {
            if (direction == FlyDirection.Left)
                return originalOffset;
            if (direction == FlyDirection.Right)
                return -originalOffset;

            return 0;
        }

        public static int CalculateInitialYOffset(int originalOffset, FlyDirection direction)
        {
            if (direction == FlyDirection.Top)
                return originalOffset;
            if (direction == FlyDirection.Bottom)
                return -originalOffset;

            return 0;
        }

        public static Rect GetElementRectangle(FrameworkElement element)
        {
            return new Rect(GetCenter(element).X, GetCenter(element).Y, element.ActualWidth, element.ActualHeight);
        }

        public static Point CalculateTranslationPoint(FlyDirection direction, FrameworkElement t)
        {
            var rootPanel = t.FindRoot<Panel>();
            rootPanel.UpdateLayout();
            var position = t.TransformToVisual(rootPanel).Transform(new Point());
            
#if SL5
            var screenWidth = BrowserScreenInformation.ClientWidth;
            var screenHeight = BrowserScreenInformation.ClientHeight;
            position.X = screenWidth/2 - rootPanel.ActualWidth/2 + position.X;
            position.Y = screenHeight/2 - rootPanel.ActualHeight/2 + position.Y;
#endif

            var translate = new Point();

            const int margin = 5;
            switch (direction)
            {
                case FlyDirection.Left:
                    translate.X = (int)(position.X + t.ActualWidth + margin);
                    break;
                case FlyDirection.Right:
                    translate.X = -(int)(rootPanel.ActualWidth - position.X + t.ActualWidth + margin);
                    break;
                case FlyDirection.Top:
                    translate.Y = (int)(position.Y + t.ActualHeight + margin);
                    break;
                case FlyDirection.Bottom:
                    translate.Y = -(int)(rootPanel.ActualHeight - position.Y + t.ActualHeight + margin);
                    break;
            }
            return translate;
        }
    }
}
